package com.frxuexi.shop

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.frxuexi.lib_base.entity.ProductBean
import com.frxuexi.lib_base.entity.ProductBeanItem
import com.frxuexi.lib_base.mvvm.BaseViewModel
import com.frxuexi.lib_base.net.ShopApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import retrofit2.http.Query
import javax.inject.Inject

@HiltViewModel
class ShopViewModel @Inject constructor(val shopApiService: ShopApiService):BaseViewModel() {
    var productSuc = MutableLiveData<ProductBean>()

    fun getProducts(type:Int, limit:Int, page:Int){
        httpRequestShop({shopApiService.getProducts(type, limit, page)},productSuc::postValue)
    }

    fun getShopsPaging():Flow<PagingData<ProductBeanItem>>{
        return Pager(PagingConfig(6), pagingSourceFactory = {ShopDataSource(shopApiService)}).flow.cachedIn(viewModelScope)
    }
}